/**
 * @author zhangluping <13101501316@163.com>
 * @createTime 2018-10-23 08:52:46
 * @description
 */
import $http from '../utils/http.js'
import { Notification } from 'atelement'
// actions
const GET_INFORMATION = 'GET_INFORMATION'
const DELETE_SERVICE = 'DELETE_SERVICE'
const CHOOSE = 'CHOOSE'
const GET_SERVICES = 'GET_SERVICES'
const GET_STATUS = 'GET_STATUS'
// mutations
const SET_SERVICES = 'SET_SERVICES'
const SET_LOADING = 'SET_LOADING'
const SET_DEVICE_LOADING = 'SET_DEVICE_LOADING'
const SET_STATISTICS = 'SET_STATISTICS'
const SET_LIST = 'SET_LIST'
const SET_SINGLE = 'SET_SINGLE'
const SET_DATE_RANGE = 'SET_DATE_RANGE'
export default {
  namespaced: true,
  state: {
    // 服务最近5次状态
    services: [],
    loading: true,
    // 所有服务列表
    list: [],
    dateRange: '',
    // 监测统计
    statistics: {}
  },
  getters: {
    loading: state => state.loading,
    services: state => {
      for (let service of state.services) {
        for (let device of service.devices) {
          device.cpu_pre_a = [{
            symbol: 'none',
            name: 'cpu使用率',
            type: 'line',
            data: device.cpu_pre,
            areaStyle: { color: '#f8f8f8'
            },
            itemStyle: {
              normal: {
                color: '#2B97EF',
                lineStyle: {
                  color: '#2B97EF'
                }
              }
            },
            lineStyle: {
              normal: {
                width: 1
              }
            }
          }]
          device.memory_pre_a = [{
            symbol: 'none',
            name: '内存使用情况',
            type: 'line',
            data: device.memory_pre,
            areaStyle: { color: '#f8f8f8' },
            itemStyle: {
              normal: {
                color: '#A03FBC',
                lineStyle: {
                  color: '#A03FBC'
                }
              }
            },
            lineStyle: {
              normal: {
                width: 1
              }
            }
          }]
          device.disk_IO_a = [{
            symbol: 'none',
            name: '磁盘读',
            type: 'line',
            data: device.read,
            areaStyle: { color: '#f8f8f8' },
            itemStyle: {
              normal: {
                color: '#A74FC0',
                lineStyle: {
                  color: '#A74FC0'
                }
              }
            },
            lineStyle: {
              normal: {
                width: 1
              }
            }
          }, {
            symbol: 'none',
            name: '磁盘写',
            type: 'line',
            data: device.write,
            areaStyle: { color: '#f8f8f8' },
            itemStyle: {
              normal: {
                color: '#B46926',
                lineStyle: {
                  color: '#B46926'
                }
              }
            },
            lineStyle: {
              normal: {
                width: 1
              }
            }
          }]
        }
      }
      return state.services
    },
    statistics: state => state.statistics,
    list: state => {
      for (let item of state.list) {
        if (item.is_use === '0') {
          item.disabled = false
        } else {
          item.disabled = true
        }
      }
      return state.list
    }
  },
  actions: {
    // 服务运行状态统计 和 服务最新5次状态
    [GET_INFORMATION] ({ commit, dispatch, state }) {
      commit(SET_LOADING, true)
      $http.post('/api/operation/information').then(({ status, data, statistics, msg } = {}) => {
        if (status === 'success') {
          let time = {}
          dispatch(GET_SERVICES)
          commit(SET_SERVICES, data)
          commit(SET_STATISTICS, statistics)
          // 给时间选择器赋默认值
          for (let service of state.services) {
            time[service.id] = ''
          }

          commit('SET_DATE_RANGE', time)
          commit(SET_LOADING, false)
        } else if (status === 'fail') {
          commit(SET_LOADING, false)
          Notification.error({
            title: '查询服务状态信息失败',
            message: msg
          })
        } else if (status === 'notallow') {
          commit(SET_LOADING, false)
          Notification.error({
            title: '不具备操作权限'
          })
        }
      })
    },
    // 删除一条服务
    [DELETE_SERVICE] ({ dispatch, commit, state }, value) {
      commit(SET_LOADING, true)
      $http.post('/api/operation/delete', value).then(({ status, msg } = {}) => {
        if (status === 'success') {
          // 给时间选择器赋默认值
          dispatch(GET_INFORMATION)
        } else if (status === 'fail') {
          Notification.error({
            title: msg
          })
          commit(SET_LOADING, false)
        } else if (status === 'notallow') {
          commit(SET_LOADING, false)
          Notification.error({
            title: '不具备操作权限'
          })
        }
      })
    },
    // 获取所有的服务列表
    [GET_SERVICES] ({ commit }) {
      commit(SET_LOADING, true)
      $http.get('/api/operation/get').then(({ status, msg, data } = {}) => {
        if (status === 'success') {
          commit(SET_LIST, data)
          commit(SET_LOADING, true)
        } else if (status === 'fail') {
          Notification.error({
            title: msg
          })
          commit(SET_LOADING, false)
        } else if (status === 'notallow') {
          commit(SET_LOADING, false)
          Notification.error({
            title: '不具备操作权限'
          })
        }
      })
    },
    // 添加要监测的网站
    [CHOOSE] ({ commit, dispatch, state }, value) {
      commit('SET_LOADING', true)
      $http.post('/api/operation/update', value).then(({ status, msg, data } = {}) => {
        if (status === 'success') {
          dispatch(GET_INFORMATION)
        } else if (status === 'fail') {
          commit('SET_LOADING', false)
          Notification.error({
            title: msg
          })
        } else if (status === 'notallow') {
          commit(SET_LOADING, false)
          Notification.error({
            title: '不具备操作权限'
          })
        }
      })
    },
    // 更改时间 查询某服务的服务运行环境
    [GET_STATUS] ({ commit, dispath, state }, value) {
      commit(SET_LOADING, true)
      $http.post('/api/operation/status', value).then(({ status, msg, devices, deviceStatus, id } = {}) => {
        if (status === 'success') {
          for (let i in state.services) {
            if (state.services[i].id === id) {
              commit(SET_SINGLE, [devices, deviceStatus, id])
              break
            }
          }
          commit(SET_LOADING, false)
        } else if (status === 'fail') {
          Notification.error({
            title: msg
          })
          commit(SET_LOADING, false)
        } else if (status === 'notallow') {
          commit(SET_LOADING, false)
          Notification.error({
            title: '不具备操作权限'
          })
        }
      })
    }
  },
  mutations: {
    [SET_SERVICES] (state, value) {
      state.services = value
    },
    [SET_STATISTICS] (state, value) {
      state.statistics = value
    },
    [SET_DEVICE_LOADING] (state, value) {
      state.deviceLoading = value
    },
    [SET_LOADING] (state, value) {
      state.loading = value
    },
    [SET_LIST] (state, value) {
      state.list = value
    },
    [SET_DATE_RANGE] (state, value) {
      state.dateRange = value
    },
    [SET_SINGLE] (state, value) {
      for (let i in state.services) {
        if (state.services[i].id === value[2]) {
          state.services[i].devices = value[0]
          state.services[i].deviceStatus = value[1]
        }
      }
    }
  }
}
